package com.union.pay.config;

import com.union.pay.properties.PayProperties;
import com.union.pay.ccb.config.CcbPayConfig;
import com.union.pay.ccb.service.CcbPayService;
import com.union.pay.ccb.service.impl.CcbPayServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

public class CcbPayAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(CcbPayService.class)
    public CcbPayService ccbPayService(PayProperties properties) {
        final CcbPayServiceImpl ccbPayService = new CcbPayServiceImpl();
        CcbPayConfig ccbConfig = new CcbPayConfig();
        // 支付字段
        ccbConfig.setAppId(StringUtils.trimToNull(properties.getCcb().getAppId()));
        ccbConfig.setUrl(StringUtils.trimToNull(properties.getCcb().getUrl()));
        ccbConfig.setMerchantId(StringUtils.trimToNull(properties.getCcb().getMerchantId()));
        ccbConfig.setPosId(StringUtils.trimToNull(properties.getCcb().getPosId()));
        ccbConfig.setBranchId(StringUtils.trimToNull(properties.getCcb().getBranchId()));
        ccbConfig.setCurCode(StringUtils.trimToNull(properties.getCcb().getCurCode()));
        ccbConfig.setTxCode(StringUtils.trimToNull(properties.getCcb().getTxCode()));
        ccbConfig.setType(StringUtils.trimToNull(properties.getCcb().getType()));
        ccbConfig.setPub(StringUtils.trimToNull(properties.getCcb().getPub()));
        ccbConfig.setPublicKey(StringUtils.trimToNull(properties.getCcb().getPublicKey()));
        ccbConfig.setGateway(StringUtils.trimToNull(properties.getCcb().getGateway()));


        // 退款字段
        ccbConfig.setRefundUrl(StringUtils.trimToNull(properties.getCcb().getRefundUrl()));
        ccbConfig.setUserId(StringUtils.trimToNull(properties.getCcb().getUserId()));
        ccbConfig.setTradePassword(StringUtils.trimToNull(properties.getCcb().getTradePassword()));
        ccbConfig.setCertFilePath(StringUtils.trimToNull(properties.getCcb().getCertFilePath()));
        ccbConfig.setCertPassword(StringUtils.trimToNull(properties.getCcb().getCertPassword()));
        ccbConfig.setConfigFilePath(StringUtils.trimToNull(properties.getCcb().getConfigFilePath()));
        ccbConfig.setLanguage(StringUtils.trimToNull(properties.getCcb().getLanguage()));


        ccbPayService.setPayConfig(ccbConfig);
        return ccbPayService;
    }
}
